var express = require('express')
var connection = require('../db/db_utill');

var router = express.Router()

// 编辑话题
router.post('/edit_topic', function (req, res) {
  //获取话题内容
  const body = req.body
  const topicInfo = [body.title, body.html_content, body.topic_pic, body.topic_category, body.topic_id]
  connection.query(`
  update topic_info
  set topic_title=?,
  topic_content=?,
  topic_pic=?,
  topic_category=? where topic_id = ?`, topicInfo, function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      const string = JSON.stringify(results);
      const jsons = JSON.parse(string)[0];
      return res.json({
        status: 200,
        jsons
      })
    }
  });
})

// 删除话题
router.post('/delete_topic', function (req, res) {
  //获取话题内容
  const topicId = req.body.topicId
  connection.query(`
  delete from agree_topic where topic_id=?;
  delete from favorite_topic where topic_id=?;
  delete from topic_comment where topic_id=?;
  delete from topic_info where topic_id=?
  `, [topicId, topicId, topicId, topicId], function (error, results, fields) {
    if (error) {
      throw error;
    } else {
      return res.json({
        status: 201,
        message: '删除成功！'
      })
    }
  });
})

module.exports = router;